package leetcode100;

import java.util.Arrays;

public class lc033 {
    public static void main(String[] args) {
        int[] nums = new int[] {1,2,3};
        int[] res = new lc033().searchRange(nums,1);
        System.out.println("Arrays.toString(res) = " + Arrays.toString(res));

    }

    public int[] searchRange(int[] nums, int target) {
        int[] res = new int[] {-1,-1};
        int left = 0;
        int right = nums.length-1;
        int mid = 0;
        while(left<=right) {
            mid = (left+right)/2;
            if (nums[mid]==target) {
                System.out.println("mid = " + mid);

                int i=mid;
                while(i>=0 && nums[i]==target) {
                    i--;
                }
                res[0] = ++i;
                i=mid;
                while(i<nums.length&&nums[i]==target) {
                    i++;
                }
                res[1] = --i;
                break;
            }
            if (target<=nums[mid] ) {
                right=mid-1;
            } else {
                left=mid+1;
            }
        }


        return res;
    }

}
